package com.yc.api2;

import java.util.Objects;

public class StringDemo {
	public static void main(String[] args) throws ClassNotFoundException, CloneNotSupportedException {
		Person p = new Person("张三",20,'男');
		Person p1 = new Person("张三",20,'男');
		System.out.println(p.toString());
		System.out.println(p);
		System.out.println(p.hashCode());
		System.out.println(p1.hashCode());
		
		// equals
		System.out.println(p == p1);		// false   对比对象的地址, 表示是否是同一对象
		System.out.println(p.equals(p1));   // true    对比对象的内容, 表示内容是否相同
		
		// getClass
		System.out.println(p.getClass());
		// 获取类对象的方法
		Class cls1 = p.getClass();   // 通过对象获取
		Class cls2 = Person.class;   // 通过类属性获取
		Class cls3 = Class.forName("com.yc.api2.Person"); // 通过forName方法
		System.out.println(cls1 == cls2);
		System.out.println(cls3 == cls2);
		System.out.println(cls1.hashCode());
		System.out.println(cls3.hashCode());
		
		Person p2 = (Person) p.clone();
		
		System.out.println(p2);
		
		// 包装器类型
		int a = 100;
		// 手动装箱
		Integer a1 = new Integer(a);
		Integer a2 = Integer.valueOf(a);
		long b = 9999;
		Long b1 = new Long(b);
		Long b2 = Long.valueOf(b);
		// 手动拆箱
		int a11 = a1.intValue();
		byte a12 = a1.byteValue();
		long a13 = a1.longValue();
		// 自动装箱
		Integer aa1 = a;
		// 自动拆箱
		a = aa1;
		
		System.out.println("int 的最小值" + Integer.MIN_VALUE);
		System.out.println("int 的最大值" + Integer.MAX_VALUE);
		
		Character c1 = 'A';
		Character c2 = '1';
		
		System.out.println("是否是小写"+Character.isLowerCase(c1));
		System.out.println("是否是数字"+Character.isDigit(c2));
		
		// 128 陷阱
		Integer i = 127;
		Integer j = 127;
		System.out.println(i == j);
		i = 128;
		j = 128;
		System.out.println(i == j);
		System.out.println(i.equals(j));
		
	}

}

class Person implements Cloneable{
	String name;
	int age;
	char gender;
	
	public Person(String name, int age, char gender) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
	
//	public boolean equals(Object o) {
//		Person p = (Person) o;
//		return p.name.equals(this.name)
//				&& p.getAge() == this.age
//				&& p.gender==this.gender;
//	}
	// 重写方法的作用域只能扩大不能缩小
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	public String toString() {
		return "name = " + name +
				",age = " + age +
				",gender = " + gender;
	}
	
	@Override
	public int hashCode() {
		return Objects.hash(age, gender, name);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		return age == other.age && gender == other.gender && Objects.equals(name, other.name);
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	
}
